/**
 * @file app_http.c
 * @author Ai-Thinker Seahi
 * @brief
 * @version 0.1
 * @date 2021-09-22
 *
 * @copyright Copyright (c) 2021
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_err.h"
#include "esp_log.h"
#include "app_http.h"
#include "esp_tls.h"

static char* TAG = "APP Http";
esp_http_client_handle_t http_client;
char http_user_data[1024] = { 0 };
bool http_data_update_flag = true;
/**
 * @brief http 配置函数
 *
 */
static void app_http_client_config(void)
{
    esp_http_client_config_t app_config = {
        // .host = My_http_host,
        // .path = My_http_path,
         //.query = "unescape=1&version=v6&appid=17769781&appsecret=cJ1P9bxc",
         .url = MY_http_url,
    };
    http_client = esp_http_client_init(&app_config);

}
/**
 * @brief get 请求
 *
 */
static void http_get_request(void)
{
    esp_err_t err;

    esp_http_client_set_method(http_client, HTTP_METHOD_GET);

    err = esp_http_client_perform(http_client);
    if (err == ESP_OK)
        ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",
               esp_http_client_get_status_code(http_client),
               esp_http_client_get_content_length(http_client));
    else {
        ESP_LOGE(TAG, "Http Get repuest failed");
        return;
    }

    int data_size = esp_http_client_read_response(http_client, http_user_data, 1024);

    ESP_LOGW(TAG, "%.*s", data_size, http_user_data);
    esp_http_client_close(http_client);
}
/**
 * @brief http
 *
 * @param arg
 */
void http_request_weather(void* arg)
{
    app_http_client_config();
    http_get_request();
    uint64_t timer_num = 0;

    for (;;) {
        //判断3小时
        vTaskDelay(1000 / portTICK_RATE_MS);
        if (timer_num > 10800) {
            http_get_request();
            http_data_update_flag = true;
            timer_num = 0;
        }
        timer_num++;
    }
}